import request from '../../../utils/request';
import {
  findAll,
  deptsTree,
  addBegin,
  deptDetails,
  editForm,
  deleteMothed,
} from '@/services/organizationSer';
import {checkList}from '@/utils/utils'
export default {
  namepace: 'Organization',
  state: {
    pagination: {},
  },
  effects: {
    *addBegin({ payload, callback }, { call, put }) {
      const formData = payload.values;
      const addNow = yield call(addBegin, formData);

      if (callback && typeof callback === 'function') {
        callback(addNow);
      }

      yield put({
        type: 'searchEnd',
      });
    },
    *findAll({ payload, callback }, { call, put }) {
      const data = yield call(findAll, payload);

      if (callback && typeof callback === 'function') {
        callback(data);
      }
      yield put({
        type: 'findAllEnd',
        payload: data,
      });
    },
    *deptsTree({ payload, callback }, { call, put }) {
      const data = yield call(deptsTree, payload);
      if (callback && typeof callback === 'function') {
        callback(data);
      }
      yield put({
        type: 'deptsTreeEnd',
        payload: data,
      });
    },
    *detailsSearch({ payload, callback }, { call, put }) {
      const data = yield call(deptDetails, payload);

      if (callback && typeof callback === 'function') {
        callback(data);
      }

      yield put({
        type: 'detailsEnd',
        payload: data,
      });
    },
    *editBegin({ payload, callback }, { call, put }) {
      const editNow = yield call(editForm, payload);

      if (callback && typeof callback === 'function') {
        callback(editNow);
      }

      yield put({
        type: 'searchEnd',
      });
    },
    *deleteBegin({ payload, callback }, { call, put }) {
      const deleteNow = yield call(deleteMothed, payload);

      if (callback && typeof callback === 'function') {
        callback(deleteNow);
      }

      return false;
    },
  },
  reducers: {
    searchEnd(state, { payload: payload }) {
      return {
        dataSource: payload,
        ...state,
      };
    },
    //查询租户列表
    findAllEnd(state, { payload: payload }) {
      return {
        treeData: payload,
        ...state,
      };
    },
    //查询组织机构树
    deleteEnd(state, { payload: payload }) {
      return {
        ...state,
      };
    },
    //详情
    detailsEnd(state, { payload: payload }) {
      return {
        detailsData: payload,
        ...state,
      };
    },
  },
};
